//
//  HomeButtons.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/5/6.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class HomeButtons: UITableViewCell {

    
    @IBOutlet weak var imgView1: UIImageView!
    
    @IBOutlet weak var imgView2: UIImageView!
    
    @IBOutlet weak var imgView3: UIImageView!
    
    @IBOutlet weak var imgView4: UIImageView!
    
    var navigationController: UINavigationController?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
//        let imgName = "zixun"
//        let imgView = UIImageView.init(image: UIImage(named: imgName))
//        imageView.frame = CGRect.init(x: 0, y: 0, width: 60, height: 60)
//        button1.addSubview(imgView)
        
        imgView1.image = UIImage(named: "kuaixun")
        imgView2.image = UIImage(named: "zixun")
        imgView3.image = UIImage(named: "xianhuo")
        imgView4.image = UIImage(named: "tongxunlu")
        
        imgView1.isUserInteractionEnabled = true
        imgView2.isUserInteractionEnabled = true
        imgView3.isUserInteractionEnabled = true
        imgView4.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action:#selector(HomeButtons.onTap(_:)))
//        imgView1.addGestureRecognizer(tap)
//        imgView2.addGestureRecognizer(tap)
//        imgView3.addGestureRecognizer(tap)
//        imgView4.addGestureRecognizer(tap)
        self.contentView.addGestureRecognizer(tap)
        self.selectionStyle = .none
    }
    
    @objc func  onTap(_ ges: UITapGestureRecognizer) -> Void {
        let locs = ges.location(in: self.contentView)
        
        if locs.x <= Const.SCREEN_WIDTH/4 {
            if !User.isLogin() {
                let vc:LoginVC = UIStoryboard.init(name: "User", bundle: Bundle.main).instantiateViewController(withIdentifier: LoginVC.VCID) as! LoginVC
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
            let vc = KuaixunListViewController.init()
            self.navigationController?.pushViewController(vc, animated: true)

        }else if locs.x <= Const.SCREEN_WIDTH/2 {
            let vc:ZixunListVC = UIStoryboard.init(name: "zixun", bundle: Bundle.main).instantiateViewController(withIdentifier: ZixunListVC.VCID) as! ZixunListVC
            self.navigationController?.pushViewController(vc, animated: true)
        }else if locs.x <= Const.SCREEN_WIDTH * 0.75 {
            if !User.isLogin() {
                let vc:LoginVC = UIStoryboard.init(name: "User", bundle: Bundle.main).instantiateViewController(withIdentifier: LoginVC.VCID) as! LoginVC
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
            let vc:XHListVC = XHListVC.init()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }else if locs.x <= Const.SCREEN_WIDTH {
            if !User.isLogin() {
                let vc:LoginVC = UIStoryboard.init(name: "User", bundle: Bundle.main).instantiateViewController(withIdentifier: LoginVC.VCID) as! LoginVC
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
            if User.get.contacts == 0{
                let alert = UIAlertController(title: "提示", message:"您暂时没有开通该功能，是否立刻联系客服开通此功能？", preferredStyle: UIAlertController.Style.alert)
                
                let action = UIAlertAction.init(title: "拨打电话", style: .destructive, handler: { (action1) in
                    let str="telprompt://15383800066"
                    UIApplication.shared.openURL(URL.init(string: str)!)
                })
                alert.addAction(action)
                let action2 = UIAlertAction.init(title: "先等等", style: .cancel, handler:nil)
                alert.addAction(action2)
                self.navigationController?.present(alert, animated: true, completion: nil)

                return
            }
            let vc:ContactsVC = ContactsVC.init()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        
    }
    
    func height() -> CGFloat{
        return 150
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }

}
